unit clsReferencia;

interface

uses Dm, dbtables, grids, clsproduto;

type
  TReferencia = class(TObject)
  public
    Ref_Codigo: string;
    function Recuperar(pro_codigo: string): boolean;
    function RecuperarReferencia(pro_codigo: string): boolean;
    procedure Gravar(pro_codigo, ref_codigo: string);
    procedure Excluir(pro_codigo: string);
    procedure Consultar(var sg: TStringGrid; pro_codigo: string);
  end;

implementation

procedure TReferencia.Gravar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into referencia values (:a,:b)');
  tq.Params[0].asstring := pro_codigo;
  tq.params[1].asstring := ref_codigo;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TReferencia.Recuperar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from referencia where pro_codigo=:a');
  tq.Params[0].asstring := pro_codigo;
  tq.open;
  Ref_Codigo := tq.FieldByName('Ref_Codigo').AsString;
  if not tq.eof then result := true else result := false;
  tq.Close;
  tq.Free;
end;

function TReferencia.RecuperarReferencia;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from referencia where ref_codigo=:a');
  tq.Params[0].asstring := pro_codigo;
  tq.open;
  if not tq.eof then result := true else result := false;
  tq.Close;
  tq.Free;
end;

procedure TReferencia.Excluir;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from referencia where pro_codigo=:a');
  tq.Params[0].asstring := pro_codigo;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TReferencia.Consultar;
var
  tq: TQuery;
  i: integer;
  produto: TProduto;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from referencia where pro_codigo=:a');
  tq.Params[0].asstring := pro_codigo;
  tq.open;
  i := 0;
  sg.RowCount := 2;
  produto := TProduto.Create;
  while not tq.eof do begin
    if i > 0 then sg.RowCount := sg.RowCount + 1;
    sg.cells[0, i + 1] := tq.fieldbyname('ref_codigo').asstring;
    produto.Recuperar(tq.fieldbyname('ref_codigo').asstring);
    sg.cells[1, i + 1] := produto.Pro_nome;
    i := i + 1;
    tq.next;
  end;
  produto.free;
  tq.close;
  tq.free;
end;

end.
